package com.hcrazy.manchester.app.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hcrazy.manchester.app.pojo.User;

public class SellerAuthInterceptor extends HandlerInterceptorAdapter{
	
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
		
		String context = req.getContextPath();
		String url = StringUtils.replace(req.getRequestURI(), context, "");
		if(url.equals("/seller/login")){
			return true;
		}
		if(url.equals("/seller/register")){
			return true;
		}
		User user =  null == req.getSession().getAttribute("user")?null:(User)req.getSession().getAttribute("user");
		if(null != user && null != user.getUsername() && !"".equals(user.getUsername())){
			return true;
		}
		req.getRequestDispatcher("/seller/login?url="+url).forward(req, resp);
		
		return false;
	}
	
	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object handler, ModelAndView modelAndView) throws Exception {
		
	}

}
